package com.ithema.service.impl;

import com.itheima.domain.Book;


import com.ithema.controller.Code;
import com.ithema.dao.BookMapper;
import com.ithema.exception.BusinessException;
import com.ithema.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BookServiceImpl implements BookService {
    @Autowired(required=false)//true，表示注入的时候，该bean必须存在，否则就会注入失败。
    private BookMapper bookMapper;

    @Override
    public boolean saveBook(Book book) {
        int i = bookMapper.saveBook(book);
        return i>0;
    }

    @Override
    public boolean update(Book book) {
        int update = bookMapper.update(book);
        return update>0;
    }

    @Override
    public boolean deleteBook(Integer id) {
        int i = bookMapper.deleteBook(id);
        return i>0;
    }

    @Override
    public List<Book> selectBookAll() {
        List<Book> books = bookMapper.selectBookAll();
        return books;
    }

    @Override
    public Book selectBook_Id(Integer id) {
//           此部分跟异常知识有关系
        //1   假如模拟 id为1 的自定义异常
        if (id==1){
            //throw new BusinessException(666,"请不要用你的技术挑战我的耐性！");  这时候可以升级下  调动Code类中属性的Integer值
            throw new BusinessException(Code.BUSINESS_ERROR,"请不要用你的技术挑战我的耐性！");
        }


      /*  //2   将可能出现的异常包装，转换成自定义异常
        try {
            int i=1/0;
        } catch (ArithmeticException e) {
            //throw new SystemException(666,"服务器访问超时请重试!",e); 这时候可以升级下  调动Code类中属性的Integer值
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERROR,"服务器访问超时请重试!",e);
        }*/
        Book book = bookMapper.selectBook_Id(id);
        return book;
    }
}
